package com.banciyuan.bcywebview.base.helper.push.Item;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.banciyuan.bcywebview.base.app.BaseApplication;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.push.PushFactory;
import com.banciyuan.bcywebview.biz.detail.complex.ComplexDetailActivity;
import com.banciyuan.bcywebview.biz.detail.noveldetail.newnoveldetail.NovelComplexActivity;
import com.banciyuan.bcywebview.biz.detail.post.PostDetailActivity;
import com.banciyuan.bcywebview.biz.start.StartActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;

import java.util.List;

/**
 * Created by squall on 2015/9/18.
 */
public class BasePush {

    protected int[] nums;

    protected String[] msgs;

    protected Context mContext;

    protected boolean fromInnerJump = false;

    protected String imageUrl;

    public void goPushActivity(Context mContext) {
        this.mContext = mContext;
        this.msgs = PushFactory.msgs;
        nums = new int[msgs.length];
        for (int i = 0; i < msgs.length; i++) {
            try {
                nums[i] = Integer.parseInt(msgs[i]);
            } catch (NumberFormatException e) {
                break;
            }
        }
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public void goPushActivity(Context mContext, boolean fromInnerJump) {
        this.fromInnerJump = fromInnerJump;
        goPushActivity(mContext);
    }

    protected void goDetail(Intent intent) {
        intent.setClass(mContext, ComplexDetailActivity.class);
        goActivities(intent);
    }

    protected void goGroupDetail(Intent intent) {
        intent.setClass(mContext, PostDetailActivity.class);
        goActivities(intent);
    }

    protected void goWorkDetail(Intent intent) {
        intent.setClass(mContext, NovelComplexActivity.class);
        goActivities(intent);
    }

    protected void goComment(Intent intent) {
        intent.putExtra("pushSource", true);
        goDetail(intent);
    }

    protected void goGroupComment(Intent intent) {
        intent.putExtra("pushSource", true);
        goGroupDetail(intent);
    }

    protected void goWorkComment(Intent intent) {
        intent.putExtra("pushSource", true);
        goWorkDetail(intent);
    }

    protected void goMain() {
        if (!fromInnerJump) {
            Intent intent = new Intent(mContext, StartActivity.class);
            mContext.startActivity(intent);
        }
    }

    protected void initDetailType(Intent intent, int num, int index) {
        switch (num) {
            case 0:
                intent.putExtra(MyConstants.CPIDVALUE, msgs[2 + index]);
                intent.putExtra(MyConstants.RPIDVALUE, msgs[3 + index]);
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_COSER);
                break;
            case 1:
                intent.putExtra(MyConstants.DPIDVALUE, msgs[2 + index]);
                intent.putExtra(MyConstants.RPIDVALUE, msgs[3 + index]);
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_ILLUST);
                break;
            case 2:
                intent.putExtra(MyConstants.GIDVALUE, msgs[2 + index]);
                intent.putExtra(MyConstants.POSTIDVALUE, msgs[3 + index]);
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_GROUP);
                break;
            case 3:
                intent.putExtra(MyConstants.UDIDVALUE, msgs[2 + index]);
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_DAILY);
                break;
            case 4:
                intent.putExtra(MyConstants.WPIDVALUE, msgs[2 + index]);
                intent.putExtra(MyConstants.RPIDVALUE, msgs[3 + index]);
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_WRITER);
                break;
        }
    }

    protected boolean contectStatus() {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        final List<ActivityManager.RunningTaskInfo> recentTasks = am.getRunningTasks(Integer.MAX_VALUE);
        int i = 0;
        for (; i < recentTasks.size(); i++) {
            if (recentTasks.get(i).baseActivity.toShortString().contains(mContext.getPackageName())) {
                break;
            }
        }
        return i != recentTasks.size();
    }

    protected void goActivities(Intent intent) {
        if (fromInnerJump) {
            mContext.startActivity(intent);
            return;
        }
        if (contectStatus()) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (((BaseApplication) mContext.getApplicationContext()).isAppRunningInFront()) {
                intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            }
            mContext.startActivity(intent);
        } else {
            Intent[] intents = new Intent[2];
            intents[1] = intent;

            Intent intentMain = new Intent();
            intentMain.addCategory(Intent.CATEGORY_LAUNCHER);
            intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intentMain.setClass(mContext, StartActivity.class);
            intents[0] = intentMain;

            mContext.startActivities(intents);
        }
    }
}
